package com.qining618.common.utils;

import com.qining618.common.utils.time.DateFormatUtil;
import com.qining618.common.utils.time.DateStyle;

import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author flt
 * @date 2019-08-16
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {

    private static final Pattern LINE_PATTERN = Pattern.compile("_(\\w)");
    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");

    /**
     * 隐藏手机号中间四位
     *
     * @param phone 手机号
     * @return {@code String}
     */
    public static String hidePhone(String phone) {
        if (isBlank(phone)) {
            return EMPTY;
        }
        return phone.replaceAll("(\\d{" + (phone.length() - 8) + "})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 获取uuid
     *
     * @return {@code String}
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().trim().replaceAll("-", "");
    }

    /**
     * 随机数生成
     *
     * @param len
     * @return
     */
    public static String randomNumber(int len) {
        StringBuilder no = new StringBuilder();
        int[] num = new int[len];
        for (int i = 0; i < len; i++) {
            if (i == 0) {
                num[i] = new Random().nextInt(9) + 1;
            } else {
                num[i] = new Random().nextInt(10);
            }
        }
        if (num.length > 0) {
            for (int value : num) {
                no.append(value);
            }
        }
        return no.toString();
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static String getOrderCode() {
        Date date = new Date();
        String dayCode = DateFormatUtil.formatDate(DateStyle.YYYYMMDD, date);
        String timeCode = DateFormatUtil.formatDate(DateStyle.HHMMSS, date);
        return String.format("%s%02d%s%06d", dayCode, new Random().nextInt(100), timeCode, new Random().nextInt(1000000));
    }

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = LINE_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

}
